package com.sk.bilibili.view.diy;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.sk.api.annotations.Autowired;
import com.sk.api.tool.BindTool;
import com.sk.bilibili.R;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.callBack.ThemClickCallBack;
import com.sk.bilibili.databinding.ViewThemUpdateBinding;
import com.sk.bilibili.utils.DensityUtils;
import com.sk.bilibili.utils.TimeUtils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ThemUpdateView extends ConstraintLayout implements View.OnClickListener {

    public ThemUpdateView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bind = ViewThemUpdateBinding.inflate(LayoutInflater.from(context), this, true);
        initData();
        initView();
        bindEvent();
    }

    private void initData() {
        pink = getResources().getColor(R.color.pink, getContext().getTheme());
        white = getResources().getColor(R.color.white, getContext().getTheme());
    }

    private OnWeekClickListener listener;
    private int pink;
    private int white;

    public void setListener(OnWeekClickListener listener) {
        this.listener = listener;
    }

    public void setThemClickCallBack(ThemClickCallBack themClickCallBack){
        bind.gvShow.getUpdateGridAdapter().setThemClickCallBack(themClickCallBack);
    }

    public void setThems(List<Them> list){
        bind.gvShow.getUpdateGridAdapter().setList(list);
    }

    private void bindEvent() {
        for (int i = 0; i < updateText.length; i++) {
            updateText[i].setOnClickListener(this);
        }
    }

    private TextView[] updateText;
    private String[] weeks = "一,二,三,四,五,六,日".split(",");

    private void initView() {
        updateText = new TextView[bind.llShow.getChildCount()];
        for (int i = 0; i < updateText.length; i++) {
            updateText[i]= (TextView) bind.llShow.getChildAt(i);
        }
        int week = TimeUtils.getWeek();
        onClick(updateText[week]);
    }

    private ViewThemUpdateBinding bind;

    @Override
    public void onClick(View v) {
        for (int i = 0; i < updateText.length; i++) {
            boolean check = v == updateText[i];
            updateText[i].setBackgroundResource(check?R.drawable.update_bg:0);
            updateText[i].setTextColor(check?white:pink);
            updateText[i].setText(check?"周"+weeks[i]:weeks[i]);
            if(check&&listener!=null)listener.onWeekClick(i);
        }
    }

    public interface OnWeekClickListener {

        void onWeekClick(int week);

    }

}